package com.hh.hot100;



import java.util.Deque;
import java.util.LinkedList;

/**
 * @Author hanghao
 * @Date 2023/7/3 14:27
 * @Version 1.0
 * 对称二叉树
 * easy
 */

public class No101 {
    public boolean isSymmetric(TreeNode root) {
        Deque<TreeNode> deque = new LinkedList<>();
        deque.offerFirst(root.left);
        deque.offerLast(root.right);
        while (!deque.isEmpty()) {
           TreeNode leftNode = deque.pollFirst();
            TreeNode rightNode = deque.pollLast();
            if(leftNode == null && rightNode == null) continue;   //为啥这边用continue？ 因为这两 都没有左右子树 就不用再放东西在双端队列了
            if(leftNode == null || rightNode == null || leftNode.val != rightNode.val) {
                return false;
            }
            deque.offerFirst(leftNode.left);
            deque.offerFirst(leftNode.right);
            deque.offerLast(rightNode.right);
            deque.offerLast(rightNode.left);
        }
        return true;
    }
}
